package com.umeox.watch.moto.dataservice.proxy;

import android.os.IBinder;
import android.os.RemoteException;

import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.interfaces.IChat;

import net.vidageek.mirror.dsl.Mirror;

public class ChatProxy implements IChat {
    private IBinder mRemote;

    private ChatProxy(IBinder remote) {
        mRemote = remote;
    }


    public static IChat fetchBinder() {
        try {
            return new ChatProxy((IBinder) new Mirror().on("android.os.ServiceManager").invoke().method("getService").withArgs(BuildConfig.chatBinderName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IBinder asBinder() {
        return mRemote;
    }

    @Override
    public byte[] getChatMsg(String deviceId, String fid, long lastMessageId) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        byte[] result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(deviceId);
            data.writeString(fid);
            data.writeLong(lastMessageId);
            mRemote.transact(TRANSACTION_GETCHATMSG, data, reply, 0);
            reply.readException();
            result = reply.createByteArray();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public String sendChatMsg(String fid, String tid, int type, long msgTime, int msgType, String msgBody) throws Exception {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        String result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(fid);
            data.writeString(tid);
            data.writeInt(type);
            data.writeLong(msgTime);
            data.writeInt(msgType);
            data.writeString(msgBody);
            mRemote.transact(TRANSACTION_SENDCHATMSG, data, reply, 0);
            reply.readException();
            result = reply.readString();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public String uploadFile(String filePath) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        String result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(filePath);
            mRemote.transact(TRANSACTION_UPLOADFILE, data, reply, 0);
            reply.readException();
            result = reply.readString();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public boolean downloadFile(String url, String filePath) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(url);
            data.writeString(filePath);
            mRemote.transact(TRANSACTION_DOWNLOADFILE, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }
}
